package so.sao.code.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.context.request.async.DeferredResult;

import so.sao.sso.bind.CurrentUser;
import so.sao.top.core.util.spring.ExtendProfiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Profile({ ExtendProfiles.EXCEPT_PRODUCTION })
@Configuration
@EnableSwagger2
public class Swagger2 {
	@Bean
	public Docket isvApi() {
		return new Docket(DocumentationType.SWAGGER_2).groupName("isv")
				.globalOperationParameters(getGlobalOperationParameters()).apiInfo(isvApiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("so.sao.code.service.web.isv")).paths(PathSelectors.any())
				.build().ignoredParameterTypes(CurrentUser.class);
	}

	@Bean
	public Docket portalApi() {
		return new Docket(DocumentationType.SWAGGER_2).genericModelSubstitutes(DeferredResult.class).groupName("portal")
				.globalOperationParameters(getGlobalOperationParameters())
				.ignoredParameterTypes(CurrentUser.class).apiInfo(portalApiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("so.sao.code.service.web.portal"))
				.paths(PathSelectors.any()).build();
	}
//	@Bean
//	public Docket apiApi() {
//		return new Docket(DocumentationType.SWAGGER_2).genericModelSubstitutes(DeferredResult.class).groupName("api")
//				/*.globalOperationParameters(getGlobalOperationParameters())*/
//				/*.ignoredParameterTypes(CurrentUser.class)*/.apiInfo(apiApiInfo()).select()
//				.apis(RequestHandlerSelectors.basePackage("so.sao.code.service.web.api"))
//				.paths(PathSelectors.any()).build();
//	}

	private ApiInfo isvApiInfo() {
		Contact contact = new Contact("junhua.cui", "http://www.sao.so", "junhua.cui@sao.so");
		return new ApiInfoBuilder().title("code service isv APIs").description("提供第三方服务调用的接口").license("透云TOP码管理平台")
				.contact(contact).version("1.0").build();
	}

	private ApiInfo portalApiInfo() {
		Contact contact = new Contact("junhua.cui", "http://www.sao.so", "junhua.cui@sao.so");
		return new ApiInfoBuilder().title("code service portal APIs").description("提供给管理页面h5的接口").license("透云TOP码管理平台")
				.contact(contact).version("1.0").build();
	}

	private List<Parameter> getGlobalOperationParameters() {
		List<Parameter> list = new ArrayList<Parameter>();
		Parameter auth = new ParameterBuilder().name("Authorization").description("Authorization")
				.modelRef(new ModelRef("string")).parameterType("header").defaultValue("Bearer ").required(true)
				.build();
		list.add(auth);
		return list;
	}

}
